package com.zd.building.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.zd.building.BuildingVo;
import com.zd.building.domain.Building;
import com.zd.building.service.BuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼宇Controller
 * 
 * @author fxiao
 * @date 2020-08-12
 */
@Controller
@RequestMapping("/place/building")
public class BuildingController extends BaseController
{
    private String prefix = "place/building";

    @Autowired
    private BuildingService buildingService;

    @RequiresPermissions("place:building:view")
    @GetMapping()
    public String building()
    {
        return prefix + "/building";
    }

    /**
     * 查询楼宇列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Building building)
    {
        startPage();
        List<BuildingVo> list = buildingService.selectBuildingList(building);
        return getDataTable(list);
    }
    @PostMapping("checkunique")
    @ResponseBody
    public Integer checkUnique(Building building){
        return buildingService.checkUnique(building);
    }

    /**
     * 导出楼宇列表
     */
    @RequiresPermissions("place:building:export")
    @Log(title = "楼宇", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Building building)
    {
        List<BuildingVo> list = buildingService.selectBuildingList(building);
        ExcelUtil<BuildingVo> util = new ExcelUtil<BuildingVo>(BuildingVo.class);
        return util.exportExcel(list, "楼宇信息");
    }

    /**
     * 新增楼宇
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存楼宇
     */
    @RequiresPermissions("place:building:add")
    @Log(title = "楼宇", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Building building)
    {
        return toAjax(buildingService.insertBuilding(building));
    }

    /**
     * 新增保存兄弟楼宇
     * @param id 参考楼id
     * @param mmap
     * @return
     */
    @RequiresPermissions("place:building:add")
    @Log(title = "楼宇", businessType = BusinessType.INSERT)
    @GetMapping("/addBrothers/{id}")
    public String addSaveBrothers(@PathVariable(value ="id") String id,ModelMap mmap)
    {
        Building building = buildingService.selectBuildingById(id);
        mmap.put("building",building);
        return prefix + "/addBrothers";
    }

    /**
     * 批量新增保存楼宇
     */
    @RequiresPermissions("place:building:add")
    @Log(title = "楼宇", businessType = BusinessType.INSERT)
    @PostMapping("/addlist")
    @ResponseBody
    public AjaxResult addSaveList(@RequestBody List<Building> buildingList)
    {
        if(buildingList==null){
            throw new RuntimeException("未接受到任何楼宇参数");
        }
        return toAjax(buildingService.insertBuildingList(buildingList));
    }
    /**
     * 修改楼宇
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        Building building = buildingService.selectBuildingById(id);
        mmap.put("building", building);
        return prefix + "/edit";
    }

    /**
     * 修改保存楼宇
     */
    @RequiresPermissions("place:building:edit")
    @Log(title = "楼宇", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Building building)
    {
        return toAjax(buildingService.updateBuilding(building));
    }

    /**
     * 删除楼宇
     */
    @RequiresPermissions("place:building:remove")
    @Log(title = "楼宇", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(buildingService.deleteBuildingByIds(ids));
    }

    /**
     * 查询场所名称集合
     */
    @Log(title = "楼宇", businessType = BusinessType.DELETE)
    @PostMapping( "/csmclist")
    @ResponseBody
    public AjaxResult selectCsmcList(@RequestBody Building building)
    {
        return AjaxResult.success("查询成功",buildingService.selectCsmcList(building));
    }
}
